home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / dist / cpp.info-1 < prev    next >
Encoding:
Text File  |  1990-02-21  |  47.6 KB  |  1,255 lines

  1. Info file cpp.info, produced by Makeinfo, -*- Text -*- from input
  2. file cpp.texinfo.
  3.  
  4. This file documents the GNU C Preprocessor.
  5.  
  6. Copyright (C) 1987, 1989 Free Software Foundation, Inc.
  7.  
  8. Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.  
  12. Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided also
  14. that the entire resulting derived work is distributed under the terms
  15. of a permission notice identical to this one.
  16.  
  17. Permission is granted to copy and distribute translations of this
  18. manual into another language, under the above conditions for modified
  19. versions.
  20.  
  21.  
  22. 
  23. File: cpp.info,  Node: Top,  Next: Global Actions,  Up: (DIR)
  24.  
  25. The C Preprocessor
  26. ******************
  27.  
  28. The C preprocessor is a "macro processor" that is used automatically
  29. by the C compiler to transform your program before actual
  30. compilation.  It is called a macro processor because it allows you to
  31. define "macros", which are brief abbreviations for longer constructs.
  32.  
  33. The C preprocessor provides four separate facilities that you can use
  34. as you see fit:
  35.  
  36.    * Inclusion of header files.  These are files of declarations that
  37.      can be substituted into your program.
  38.  
  39.    * Macro expansion.  You can define "macros", which are
  40.      abbreviations for arbitrary fragments of C code, and then the C
  41.      preprocessor will replace the macros with their definitions
  42.      throughout the program.
  43.  
  44.    * Conditional compilation.  Using special preprocessor commands,
  45.      you can include or exclude parts of the program according to
  46.      various conditions.
  47.  
  48.    * Line control.  If you use a program to combine or rearrange
  49.      source files into an intermediate file which is then compiled,
  50.      you can use line control to inform the compiler of where each
  51.      source line originally came from.
  52.  
  53. C preprocessors vary in some details.  This manual discusses the GNU
  54. C preprocessor, the C Compatible Compiler Preprocessor.  The GNU C
  55. preprocessor provides a superset of the features of ANSI Standard C.
  56.  
  57. ANSI Standard C requires the rejection of many harmless constructs
  58. commonly used by today's C programs.  Such incompatibility would be
  59. inconvenient for users, so the GNU C preprocessor is configured to
  60. accept these constructs by default.  Strictly speaking, to get ANSI
  61. Standard C, you must use the options `-trigraphs', `-undef' and
  62. `-pedantic', but in practice the consequences of having strict ANSI
  63. Standard C make it undesirable to do this.  *Note Invocation::.
  64.  
  65. * Menu:
  66.  
  67. * Global Actions::    Actions made uniformly on all input files.
  68. * Commands::          General syntax of preprocessor commands.
  69. * Header Files::      How and why to use header files.
  70. * Macros::            How and why to use macros.
  71. * Conditionals::      How and why to use conditionals.
  72. * Combining Sources:: Use of line control when you combine source files.
  73. * Other Commands::    Miscellaneous preprocessor commands.
  74. * Output::            Format of output from the C preprocessor.
  75. * Invocation::        How to invoke the preprocessor; command options.
  76. * Concept Index::     Index of concepts and terms.
  77. * Index::             Index of commands, predefined macros and options.
  78.  
  79.  
  80. 
  81. File: cpp.info,  Node: Global Actions,  Next: Commands,  Prev: Top,  Up: Top
  82.  
  83. Transformations Made Globally
  84. =============================
  85.  
  86. Most C preprocessor features are inactive unless you give specific
  87. commands to request their use.  (Preprocessor commands are lines
  88. starting with `#'; *note Commands::.).  But there are three
  89. transformations that the preprocessor always makes on all the input
  90. it receives, even in the absence of commands.
  91.  
  92.    * All C comments are replaced with single spaces.
  93.  
  94.    * Backslash-Newline sequences are deleted, no matter where.  This
  95.      feature allows you to break long lines for cosmetic purposes
  96.      without changing their meaning.
  97.  
  98.    * Predefined macro names are replaced with their expansions (*note
  99.      Predefined::.).
  100.  
  101. The first two transformations are done *before* nearly all other
  102. parsing and before preprocessor commands are recognized.  Thus, for
  103. example, you can split a line cosmetically with Backslash-Newline
  104. anywhere (except when trigraphs are in use; see below).
  105.  
  106.      /*
  107.      */ # /*
  108.      */ defi\
  109.      ne FO\
  110.      O 10\
  111.      20
  112.  
  113. is equivalent into `#define FOO 1020'.  You can split even an escape
  114. sequence with Backslash-Newline.  For example, you can split
  115. `"foo\bar"' between the `\' and the `b' to get
  116.  
  117.      "foo\\
  118.      bar"
  119.  
  120. This behavior is unclean: in all other contexts, a Backslash can be
  121. inserted in a string constant as an ordinary character by writing a
  122. double Backslash, and this creates an exception.  But the ANSI C
  123. standard requires it.  (Strict ANSI C does not allow Newlines in
  124. string constants, so they do not consider this a problem.)
  125.  
  126. But there are a few exceptions to all three transformations.
  127.  
  128.    * C comments and predefined macro names are not recognized inside
  129.      a `#include' command in which the file name is delimited with
  130.      `<' and `>'.
  131.  
  132.    * C comments and predefined macro names are never recognized
  133.      within a character or string constant.  (Strictly speaking, this
  134.      is the rule, not an exception, but it is worth noting here
  135.      anyway.)
  136.  
  137.    * Backslash-Newline may not safely be used within an ANSI
  138.      ``trigraph''.  Trigraphs are converted before Backslash-Newline
  139.      is deleted.  If you write what looks like a trigraph with a
  140.      Backslash-Newline inside, the Backslash-Newline is deleted as
  141.      usual, but it is then too late to recognize the trigraph.
  142.  
  143.      This exception is relevant only if you use the `-trigraphs'
  144.      option to enable trigraph processing.  *Note Invocation::.
  145.  
  146.  
  147. 
  148. File: cpp.info,  Node: Commands,  Next: Header Files,  Prev: Global Actions,  Up: Top
  149.  
  150. Preprocessor Commands
  151. =====================
  152.  
  153. Most preprocessor features are active only if you use preprocessor
  154. commands to request their use.
  155.  
  156. Preprocessor commands are lines in your program that start with `#'. 
  157. The `#' is followed by an identifier that is the "command name".  For
  158. example, `#define' is the command that defines a macro.  Whitespace
  159. is also allowed before and after the `#'.
  160.  
  161. The set of valid command names is fixed.  Programs cannot define new
  162. preprocessor commands.
  163.  
  164. Some command names require arguments; these make up the rest of the
  165. command line and must be separated from the command name by
  166. whitespace.  For example, `#define' must be followed by a macro name
  167. and the intended expansion of the macro.
  168.  
  169. A preprocessor command cannot be more than one line in normal
  170. circumstances.  It may be split cosmetically with Backslash-Newline,
  171. but that has no effect on its meaning.  Comments containing Newlines
  172. can also divide the command into multiple lines, but the comments are
  173. changed to Spaces before the command is interpreted.  The only way a
  174. significant Newline can occur in a preprocessor command is within a
  175. string constant or character constant.  Note that most C compilers
  176. that might be applied to the output from the preprocessor do not
  177. accept string or character constants containing Newlines.
  178.  
  179. The `#' and the command name cannot come from a macro expansion.  For
  180. example, if `foo' is defined as a macro expanding to `define', that
  181. does not make `#foo' a valid preprocessor command.
  182.  
  183.  
  184. 
  185. File: cpp.info,  Node: Header Files,  Next: Macros,  Prev: Commands,  Up: Top
  186.  
  187. Header Files
  188. ============
  189.  
  190. A header file is a file containing C declarations and macro
  191. definitions (*note Macros::.) to be shared between several source
  192. files.  You request the use of a header file in your program with the
  193. C preprocessor command `#include'.
  194.  
  195. * Menu:
  196.  
  197. * Header Uses::         What header files are used for.
  198. * Include Syntax::      How to write `#include' commands.
  199. * Include Operation::   What `#include' does.
  200. * Once-Only::        Preventing multiple inclusion of one header file.
  201.  
  202.  
  203. 
  204. File: cpp.info,  Node: Header Uses,  Next: Include Syntax,  Prev: Header Files,  Up: Header Files
  205.  
  206. Uses of Header Files
  207. --------------------
  208.  
  209. Header files serve two kinds of purposes.
  210.  
  211.    * System header files declare the interfaces to parts of the
  212.      operating system.  You include them in your program to supply
  213.      the definitions you need to invoke system calls and libraries.
  214.  
  215.    * Your own header files contain declarations for interfaces
  216.      between the source files of your program.  Each time you have a
  217.      group of related declarations and macro definitions all or most
  218.      of which are needed in several different source files, it is a
  219.      good idea to create a header file for them.
  220.  
  221. Including a header file produces the same results in C compilation as
  222. copying the header file into each source file that needs it.  But
  223. such copying would be time-consuming and error-prone.  With a header
  224. file, the related declarations appear in only one place.  If they
  225. need to be changed, they can be changed in one place, and programs
  226. that include the header file will automatically use the new version
  227. when next recompiled.  The header file eliminates the labor of
  228. finding and changing all the copies as well as the risk that a
  229. failure to find one copy will result in inconsistencies within a
  230. program.
  231.  
  232. The usual convention is to give header files names that end with `.h'.
  233.  
  234.  
  235. 
  236. File: cpp.info,  Node: Include Syntax,  Next: Include Operation,  Prev: Header Uses,  Up: Header Files
  237.  
  238. The `#include' Command
  239. ----------------------
  240.  
  241. Both user and system header files are included using the preprocessor
  242. command `#include'.  It has three variants:
  243.  
  244. `#include <FILE>'
  245.      This variant is used for system header files.  It searches for a
  246.      file named FILE in a list of directories specified by you, then
  247.      in a standard list of system directories.  You specify
  248.      directories to search for header files with the command option
  249.      `-I' (*note Invocation::.).  The option `-nostdinc' inhibits
  250.      searching the standard system directories; in this case only the
  251.      directories you specify are searched.
  252.  
  253.      The parsing of this form of `#include' is slightly special
  254.      because comments are not recognized within the `<...>'.  Thus,
  255.      in `#include <x/*y>' the `/*' does not start a comment and the
  256.      command specifies inclusion of a system header file named
  257.      `x/*y'.  Of course, a header file with such a name is unlikely
  258.      to exist on Unix, where shell wildcard features would make it
  259.      hard to manipulate.
  260.  
  261.      The argument FILE may not contain a `>' character.  It may,
  262.      however, contain a `<' character.
  263.  
  264. `#include "FILE"'
  265.      This variant is used for header files of your own program.  It
  266.      searches for a file named FILE first in the current directory,
  267.      then in the same directories used for system header files.  The
  268.      current directory is the directory of the current input file. 
  269.      It is tried first because it is presumed to be the location of
  270.      the files that the current input file refers to.  (If the `-I-'
  271.      option is used, the special treatment of the current directory
  272.      is inhibited.)
  273.  
  274.      The argument FILE may not contain `"' characters.  If
  275.      backslashes occur within FILE, they are considered ordinary text
  276.      characters, not escape characters.  None of the character escape
  277.      sequences appropriate to string constants in C are processed. 
  278.      Thus, `#include "x\n\\y"' specifies a filename containing three
  279.      backslashes.  It is not clear why this behavior is ever useful,
  280.      but the ANSI standard specifies it.
  281.  
  282. `#include ANYTHING ELSE'
  283.      This variant is called a "computed #include".  Any `#include'
  284.      command whose argument does not fit the above two forms is a
  285.      computed include.  The text ANYTHING ELSE is checked for macro
  286.      calls, which are expanded (*note Macros::.).  When this is done,
  287.      the result must fit one of the above two variants.
  288.  
  289.      This feature allows you to define a macro which controls the
  290.      file name to be used at a later point in the program.  One
  291.      application of this is to allow a site-configuration file for
  292.      your program to specify the names of the system include files to
  293.      be used.  This can help in porting the program to various
  294.      operating systems in which the necessary system header files are
  295.      found in different places.
  296.  
  297.  
  298. 
  299. File: cpp.info,  Node: Include Operation,  Next: Once-Only,  Prev: Include Syntax,  Up: Header Files
  300.  
  301. How `#include' Works
  302. --------------------
  303.  
  304. The `#include' command works by directing the C preprocessor to scan
  305. the specified file as input before continuing with the rest of the
  306. current file.  The output from the preprocessor contains the output
  307. already generated, followed by the output resulting from the included
  308. file, followed by the output that comes from the text after the
  309. `#include' command.  For example, given two files as follows:
  310.  
  311.      /* File program.c */
  312.      int x;
  313.      #include "header.h"
  314.      
  315.      main ()
  316.      {
  317.        printf (test ());
  318.      }
  319.      
  320.      
  321.      /* File header.h */
  322.      char *test ();
  323.  
  324. the output generated by the C preprocessor for `program.c' as input
  325. would be
  326.  
  327.      int x;
  328.      char *test ();
  329.      
  330.      main ()
  331.      {
  332.        printf (test ());
  333.      }
  334.  
  335. Included files are not limited to declarations and macro definitions;
  336. they are merely the typical use.  Any fragment of a C program can be
  337. included from another file.  The include file could even contain the
  338. beginning of a statement that is concluded in the containing file, or
  339. the end of a statement that was started in the including file. 
  340. However, a comment or a string or character constant may not start in
  341. the included file and finish in the including file.  An unterminated
  342. comment, string constant or character constant in an included file is
  343. considered to end (with an error message) at the end of the file.
  344.  
  345. The line following the `#include' command is always treated as a
  346. separate line by the C preprocessor even if the included file lacks a
  347. final newline.
  348.  
  349.  
  350. 
  351. File: cpp.info,  Node: Once-Only,  Prev: Include Operation,  Up: Header Files
  352.  
  353. Once-Only Include Files
  354. -----------------------
  355.  
  356. Very often, one header file includes another.  It can easily result
  357. that a certain header file is included more than once.  This may lead
  358. to errors, if the header file defines structure types or typedefs,
  359. and is certainly wasteful.  Therefore, we often wish to prevent
  360. multiple inclusion of a header file.
  361.  
  362. The standard way to do this is to enclose the entire real contents of
  363. the file in a conditional, like this:
  364.  
  365.      #ifndef __FILE_FOO_SEEN__
  366.      #define __FILE_FOO_SEEN__
  367.      
  368.      THE ENTIRE FILE
  369.      
  370.      #endif /* __FILE_FOO_SEEN__ */
  371.  
  372. The macro `__FILE_FOO_SEEN__' indicates that the file has been
  373. included once already; its name should begin with `__', and should
  374. contain the name of the file to avoid accidental conflicts.
  375.  
  376. One drawback of this method is that the preprocessor must scan the
  377. input file completely in order to determine that all of it is to be
  378. ignored.  This makes compilation slower.  You can avoid the delay by
  379. inserting the following command near the beginning of file *in
  380. addition to the conditionals described above*:
  381.  
  382.      #pragma once
  383.  
  384. This command tells the GNU C preprocessor to ignore any future
  385. commands to include the same file (whichever file the `#pragma'
  386. appears in).
  387.  
  388. You should not *rely* on `#pragma once' to prevent multiple inclusion
  389. of the file.  It is just a hint, and a nonstandard one at that.  Most
  390. C compilers will ignore it entirely.  For this reason, you still need
  391. the conditionals if you want to make certain that the file's contents
  392. are not included twice.
  393.  
  394. Note that `#pragma once' works by file name; if a file has more than
  395. one name, it can be included once under each name, even in GNU CC,
  396. despite `#pragma once'.
  397.  
  398.  
  399. 
  400. File: cpp.info,  Node: Macros,  Next: Conditionals,  Prev: Header Files,  Up: Top
  401.  
  402. Macros
  403. ======
  404.  
  405. A macro is a sort of abbreviation which you can define once and then
  406. use later.  There are many complicated features associated with
  407. macros in the C preprocessor.
  408.  
  409. * Menu:
  410.  
  411. * Simple Macros::    Macros that always expand the same way.
  412. * Argument Macros::  Macros that accept arguments that are substituted
  413.                        into the macro expansion.
  414. * Predefined::       Predefined macros that are always available.
  415. * Stringification::  Macro arguments converted into string constants.
  416. * Concatenation::    Building tokens from parts taken from macro arguments.
  417. * Undefining::       Cancelling a macro's definition.
  418. * Redefining::       Changing a macro's definition.
  419. * Macro Pitfalls::   Macros can confuse the unwary.  Here we explain
  420.                        several common problems and strange features.
  421.  
  422.  
  423. 
  424. File: cpp.info,  Node: Simple Macros,  Next: Argument Macros,  Prev: Macros,  Up: Macros
  425.  
  426. Simple Macros
  427. -------------
  428.  
  429. A "simple macro" is a kind of abbreviation.  It is a name which
  430. stands for a fragment of code.
  431.  
  432. Before you can use a macro, you must "define" it explicitly with the
  433. `#define' command.  `#define' is followed by the name of the macro
  434. and then the code it should be an abbreviation for.  For example,
  435.  
  436.      #define BUFFER_SIZE 1020
  437.  
  438. defines a macro named `BUFFER_SIZE' as an abbreviation for the text
  439. `1020'.  Therefore, if somewhere after this `#define' command there
  440. comes a C statement of the form
  441.  
  442.      foo = (char *) xmalloc (BUFFER_SIZE);
  443.  
  444. then the C preprocessor will recognize and "expand" the macro
  445. `BUFFER_SIZE', resulting in
  446.  
  447.      foo = (char *) xmalloc (1020);
  448.  
  449. the definition must be a single line; however, it may not end in the
  450. middle of a multi-line string constant or character constant.
  451.  
  452. The use of all upper case for macro names is a standard convention. 
  453. Programs are easier to read when it is possible to tell at a glance
  454. which names are macros.
  455.  
  456. Normally, a macro definition must be a single line, like all C
  457. preprocessor commands.  (You can split a long macro definition
  458. cosmetically with Backslash-Newline.)  There is one exception:
  459. Newlines can be included in the macro definition if within a string
  460. or character constant.  By the same token, it is not possible for a
  461. macro definition to contain an unbalanced quote character; the
  462. definition automatically extends to include the matching quote
  463. character that ends the string or character constant.  Comments
  464. within a macro definition may contain Newlines, which make no
  465. difference since the comments are entirely replaced with Spaces
  466. regardless of their contents.
  467.  
  468. Aside from the above, there is no restriction on what can go in a
  469. macro body.  Parentheses need not balance.  The body need not
  470. resemble valid C code.  (Of course, you might get error messages from
  471. the C compiler when you use the macro.)
  472.  
  473. The C preprocessor scans your program sequentially, so macro
  474. definitions take effect at the place you write them.  Therefore, the
  475. following input to the C preprocessor
  476.  
  477.      foo = X;
  478.      #define X 4
  479.      bar = X;
  480.  
  481. produces as output
  482.  
  483.      foo = X;
  484.      
  485.      bar = 4;
  486.  
  487. After the preprocessor expands a macro name, the macro's definition
  488. body is appended to the front of the remaining input, and the check
  489. for macro calls continues.  Therefore, the macro body can contain
  490. calls to other macros.  For example, after
  491.  
  492.      #define BUFSIZE 1020
  493.      #define TABLESIZE BUFSIZE
  494.  
  495. the name `TABLESIZE' when used in the program would go through two
  496. stages of expansion, resulting ultimately in `1020'.
  497.  
  498. This is not at all the same as defining `TABLESIZE' to be `1020'. 
  499. The `#define' for `TABLESIZE' uses exactly the body you specify--in
  500. this case, `BUFSIZE'--and does not check to see whether it too is the
  501. name of a macro.  It's only when you *use* `TABLESIZE' that the
  502. result of its expansion is checked for more macro names.  *Note
  503. Cascaded Macros::.
  504.  
  505.  
  506. 
  507. File: cpp.info,  Node: Argument Macros,  Next: Predefined,  Prev: Simple Macros,  Up: Macros
  508.  
  509. Macros with Arguments
  510. ---------------------
  511.  
  512. A simple macro always stands for exactly the same text, each time it
  513. is used.  Macros can be more flexible when they accept "arguments". 
  514. Arguments are fragments of code that you supply each time the macro
  515. is used.  These fragments are included in the expansion of the macro
  516. according to the directions in the macro definition.
  517.  
  518. To define a macro that uses arguments, you write a `#define' command
  519. with a list of "argument names" in parentheses after the name of the
  520. macro.  The argument names may be any valid C identifiers, separated
  521. by commas and optionally whitespace.  The open-parenthesis must
  522. follow the macro name immediately, with no space in between.
  523.  
  524. For example, here is a macro that computes the minimum of two numeric
  525. values, as it is defined in many C programs:
  526.  
  527.      #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
  528.  
  529. (This is not the best way to define a ``minimum'' macro in GNU C. 
  530. *Note Side Effects::, for more information.)
  531.  
  532. To use a macro that expects arguments, you write the name of the
  533. macro followed by a list of "actual arguments" in parentheses.
  534. separated by commas.  The number of actual arguments you give must
  535. match the number of arguments the macro expects.   Examples of use of
  536. the macro `min' include `min (1, 2)' and `min (x + 28, *p)'.
  537.  
  538. The expansion text of the macro depends on the arguments you use. 
  539. Each of the argument names of the macro is replaced, throughout the
  540. macro definition, with the corresponding actual argument.  Using the
  541. same macro `min' defined above, `min (1, 2)' expands into
  542.  
  543.      ((1) < (2) ? (1) : (2))
  544.  
  545. where `1' has been substituted for `X' and `2' for `Y'.
  546.  
  547. Likewise, `min (x + 28, *p)' expands into
  548.  
  549.      ((x + 28) < (*p) ? (x + 28) : (*p))
  550.  
  551. Parentheses in the actual arguments must balance; a comma within
  552. parentheses does not end an argument.  However, there is no
  553. requirement for brackets or braces to balance; thus, if you want to
  554. supply `array[x = y, x + 1]' as an argument, you must write it as
  555. `array[(x = y, x + 1)]', which is equivalent C code.
  556.  
  557. After the actual arguments are substituted into the macro body, the
  558. entire result is appended to the front of the remaining input, and
  559. the check for macro calls continues.  Therefore, the actual arguments
  560. can contain calls to other macros, either with or without arguments,
  561. or even to the same macro.  The macro body can also contain calls to
  562. other macros.  For example, `min (min (a, b), c)' expands into
  563.  
  564.      ((((a) < (b) ? (a) : (b))) < (c)
  565.       ? (((a) < (b) ? (a) : (b)))
  566.       : (c))
  567.  
  568. (Line breaks shown here for clarity would not actually be generated.)
  569.  
  570. If you use the macro name followed by something other than an
  571. open-parenthesis (after ignoring any spaces, tabs and comments that
  572. follow), it is not a call to the macro, and the preprocessor leaves
  573. the name unaltered.  Therefore, it is possible for the same name to
  574. be a variable or function in your program as well as a macro, and you
  575. can choose in each instance whether to refer to the macro (if an
  576. actual argument list follows) or the variable or function (if an
  577. argument list does not follow).
  578.  
  579. Such dual use of one name could be confusing and should be avoided
  580. except when the two meanings are effectively synonymous: that is,
  581. when the name is both a macro and a function and the two have similar
  582. effects.  You can think of the name simply as a function; use of the
  583. name for purposes other than calling it (such as, to take the
  584. address) will refer to the function, while calls will expand the
  585. macro and generate better but equivalent code.  For example, you can
  586. use a function named `min' in the same source file that defines the
  587. macro.  If you write `&min' with no argument list, you refer to the
  588. function.  If you write `min (x, bb)', with an argument list, the
  589. macro is expanded.  If you write `(min) (a, bb)', where the name
  590. `min' is not followed by an open-parenthesis, the macro is not
  591. expanded, so you wind up with a call to the function `min'.
  592.  
  593. It is not allowed to define the same name as both a simple macro and
  594. a macro with arguments.
  595.  
  596. In the definition of a macro with arguments, the list of argument
  597. names must follow the macro name immediately with no space in
  598. between.  If there is a space after the macro name, the macro is
  599. defined as taking no arguments, and all the rest of the name is taken
  600. to be the expansion.  The reason for this is that it is often useful
  601. to define a macro that takes no arguments and whose definition begins
  602. with an identifier in parentheses.  This rule about spaces makes it
  603. possible for you to do either this:
  604.  
  605.      #define FOO(x) - 1 / (x)
  606.  
  607. (which defines `FOO' to take an argument and expand into minus the
  608. reciprocal of that argument) or this:
  609.  
  610.      #define BAR (x) - 1 / (x)
  611.  
  612. (which defines `BAR' to take no argument and always expand into `(x)
  613. - 1 / (x)').
  614.  
  615. Note that the *uses* of a macro with arguments can have spaces before
  616. the left parenthesis; it's the *definition* where it matters whether
  617. there is a space.
  618.  
  619.  
  620. 
  621. File: cpp.info,  Node: Predefined,  Next: Stringification,  Prev: Argument Macros,  Up: Macros
  622.  
  623. Predefined Macros
  624. -----------------
  625.  
  626. Several simple macros are predefined.  You can use them without
  627. giving definitions for them.  They fall into two classes: standard
  628. macros and system-specific macros.
  629.  
  630. * Menu:
  631.  
  632. * Standard Predefined::     Standard predefined macros.
  633. * Nonstandard Predefined::  Nonstandard predefined macros.
  634.  
  635.  
  636. 
  637. File: cpp.info,  Node: Standard Predefined,  Next: Nonstandard Predefined,  Prev: Predefined,  Up: Predefined
  638.  
  639. Standard Predefined Macros
  640. ..........................
  641.  
  642.  The standard predefined macros are available with the same meanings
  643. regardless of the machine or operating system on which you are using
  644. GNU C.  Their names all start and end with double underscores.  Those
  645. preceding `__GNUC__' in this table are standardized by ANSI C; the
  646. rest are GNU C extensions.
  647.  
  648. `__FILE__'
  649.      This macro expands to the name of the current input file, in the
  650.      form of a C string constant.
  651.  
  652. `__BASE_FILE__'
  653.      This macro expands to the name of the main input file, in the
  654.      form of a C string constant.  This is the source file that was
  655.      specified as an argument when the C compiler was invoked.
  656.  
  657. `__LINE__'
  658.      This macro expands to the current input line number, in the form
  659.      of a decimal integer constant.  While we call it a predefined
  660.      macro, it's a pretty strange macro, since its ``definition''
  661.      changes with each new line of source code.
  662.  
  663.      This and `__FILE__' are useful in generating an error message to
  664.      report an inconsistency detected by the program; the message can
  665.      state the source line at which the inconsistency was detected. 
  666.      For example,
  667.  
  668.           fprintf (stderr, "Internal error: negative string length "
  669.                            "%d at %s, line %d.",
  670.                    length, __FILE__, __LINE__);
  671.  
  672.      A `#include' command changes the expansions of `__FILE__' and
  673.      `__LINE__' to correspond to the included file.  At the end of
  674.      that file, when processing resumes on the input file that
  675.      contained the `#include' command, the expansions of `__FILE__'
  676.      and `__LINE__' revert to the values they had before the
  677.      `#include' (but `__LINE__' is then incremented by one as
  678.      processing moves to the line after the `#include').
  679.  
  680.      The expansions of both `__FILE__' and `__LINE__' are altered if
  681.      a `#line' command is used.  *Note Combining Sources::.
  682.  
  683. `__DATE__'
  684.      This macro expands to a string constant that describes the date
  685.      on which the preprocessor is being run.  The string constant
  686.      contains eleven characters and looks like `"Jan 29 1987"' or
  687.      `"Apr 1 1905"'.
  688.  
  689. `__TIME__'
  690.      This macro expands to a string constant that describes the time
  691.      at which the preprocessor is being run.  The string constant
  692.      contains eight characters and looks like `"23:59:01"'.
  693.  
  694. `__STDC__'
  695.      This macro expands to the constant 1, to signify that this is
  696.      ANSI Standard C.  (Whether that is actually true depends on what
  697.      C compiler will operate on the output from the preprocessor.)
  698.  
  699. `__GNUC__'
  700.      This macro is defined if and only if this is GNU C.  This macro
  701.      is defined only when the entire GNU C compiler is in use; if you
  702.      invoke the preprocessor directly, `__GNUC__' is undefined.
  703.  
  704. `__STRICT_ANSI__'
  705.      This macro is defined if and only if the `-ansi' switch was
  706.      specified when GNU C was invoked.  Its definition is the null
  707.      string.  This macro exists primarily to direct certain GNU
  708.      header files not to define certain traditional Unix constructs
  709.      which are incompatible with ANSI C.
  710.  
  711. `__VERSION__'
  712.      This macro expands to a string which describes the version
  713.      number of GNU C.  The string is normally a sequence of decimal
  714.      numbers separated by periods, such as `"1.18"'.  The only
  715.      reasonable use of this macro is to incorporate it into a string
  716.      constant.
  717.  
  718. `__OPTIMIZE__'
  719.      This macro is defined in optimizing compilations.  It causes
  720.      certain GNU header files to define alternative macro definitions
  721.      for some system library functions.  It is unwise to refer to or
  722.      test the definition of this macro unless you make very sure that
  723.      programs will execute with the same effect regardless.
  724.  
  725. `__CHAR_UNSIGNED__'
  726.      This macro is defined if and only if the data type `char' is
  727.      unsigned on the target machine.  It exists to cause the standard
  728.      header file `limit.h' to work correctly.  It is bad practice to
  729.      refer to this macro yourself; instead, refer to the standard
  730.      macros defined in `limit.h'.
  731.  
  732.  
  733. 
  734. File: cpp.info,  Node: Nonstandard Predefined,  Prev: Standard Predefined,  Up: Predefined
  735.  
  736. Nonstandard Predefined Macros
  737. .............................
  738.  
  739.  The C preprocessor normally has several predefined macros that vary
  740. between machines because their purpose is to indicate what type of
  741. system and machine is in use.  This manual, being for all systems and
  742. machines, cannot tell you exactly what their names are; instead, we
  743. offer a list of some typical ones.
  744.  
  745. Some nonstandard predefined macros describe the operating system in
  746. use, with more or less specificity.  For example,
  747.  
  748. `unix'
  749.      `unix' is normally predefined on all Unix systems.
  750.  
  751. `BSD'
  752.      `BSD' is predefined on recent versions of Berkeley Unix (perhaps
  753.      only in version 4.3).
  754.  
  755. Other nonstandard predefined macros describe the kind of CPU, with
  756. more or less specificity.  For example,
  757.  
  758. `vax'
  759.      `vax' is predefined on Vax computers.
  760.  
  761. `mc68000'
  762.      `mc68000' is predefined on most computers whose CPU is a
  763.      Motorola 68000, 68010 or 68020.
  764.  
  765. `m68k'
  766.      `m68k' is also predefined on most computers whose CPU is a
  767.      68000, 68010 or 68020; however, some makers use `mc68000' and
  768.      some use `m68k'.  Some predefine both names.  What happens in
  769.      GNU C depends on the system you are using it on.
  770.  
  771. `M68020'
  772.      `M68020' has been observed to be predefined on some systems that
  773.      use 68020 CPUs--in addition to `mc68000' and `m68k' that are
  774.      less specific.
  775.  
  776. `ns32000'
  777.      `ns32000' is predefined on computers which use the National
  778.      Semiconductor 32000 series CPU.
  779.  
  780. Yet other nonstandard predefined macros describe the manufacturer of
  781. the system.  For example,
  782.  
  783. `sun'
  784.      `sun' is predefined on all models of Sun computers.
  785.  
  786. `pyr'
  787.      `pyr' is predefined on all models of Pyramid computers.
  788.  
  789. `sequent'
  790.      `sequent' is predefined on all models of Sequent computers.
  791.  
  792. These predefined symbols are not only nonstandard, they are contrary
  793. to the ANSI standard because their names do not start with underscores.
  794. Therefore, the option `-ansi' inhibits the definition of these symbols.
  795.  
  796. This tends to make `-ansi' useless, since many programs depend on the
  797. customary nonstandard predefined symbols.  Even system header files
  798. check them and will generate incorrect declarations if they do not
  799. find the names that are expected.  You might think that the header
  800. files supplied for the Uglix computer would not need to test what
  801. machine they are running on, because they can simply assume it is the
  802. Uglix; but often they do, and they do so using the customary names. 
  803. As a result, very few C programs will compile with `-ansi'.  We
  804. intend to avoid such problems on the GNU system.
  805.  
  806. What, then, should you do in an ANSI C program to test the type of
  807. machine it is to run on?
  808.  
  809. GNU C offers a parallel series of symbols for this purpose, whose
  810. names are made from the customary ones by adding `__' at the
  811. beginning and end.  Thus, the symbol `__vax__' would be available on
  812. a vax, and so on.
  813.  
  814. The set of nonstandard predefined names in the GNU C preprocessor is
  815. controlled by the macro `CPP_PREDEFINES', which should be a string
  816. containing `-D' options, separated by spaces.  For example, on the
  817. Sun 3, we use the following definition:
  818.  
  819.      #define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k"
  820.  
  821.  
  822. 
  823. File: cpp.info,  Node: Stringification,  Next: Concatenation,  Prev: Predefined,  Up: Macros
  824.  
  825. Stringification
  826. ---------------
  827.  
  828. "Stringification" means turning a code fragment into a string
  829. constant whose contents are the text for the code fragment.  For
  830. example, stringifying `foo (z)' results in `"foo (z)"'.
  831.  
  832. In the C preprocessor, stringification is an option available when
  833. macro arguments are substituted into the macro definition.  In the
  834. body of the definition, when an argument name appears, the character
  835. `#' before the name specifies stringification of the corresponding
  836. actual argument when it is substituted at that point in the
  837. definition.  The same argument may be substituted in other places in
  838. the definition without stringification if the argument name appears
  839. in those places with no `#'.
  840.  
  841. Here is an example of a macro definition that uses stringification:
  842.  
  843.      #define WARN_IF(EXP) \
  844.      do { if (EXP) fprintf (stderr, "Warning: " #EXP "\n"); } while (0)
  845.  
  846. Here the actual argument for `EXP' is substituted once as given, into
  847. the `if' statement, and once as stringified, into the argument to
  848. `fprintf'.  The `do' and `while (0)' are a kludge to make it possible
  849. to write `WARN_IF (ARG);', which the resemblance of `WARN_IF' to a
  850. function would make C programmers want to do; *note Swallow
  851. Semicolon::.).
  852.  
  853. The stringification feature is limited to transforming one macro
  854. argument into one string constant: there is no way to combine the
  855. argument with other text and then stringify it all together.  But the
  856. example above shows how an equivalent result can be obtained in ANSI
  857. Standard C using the feature that adjacent string constants are
  858. concatenated as one string constant.  The preprocessor stringifies
  859. `EXP''s actual argument into a separate string constant, resulting in
  860. text like
  861.  
  862.      do { if (x == 0) fprintf (stderr, "Warning: " "x == 0" "\n"); } while (0)
  863.  
  864. but the C compiler then sees three consecutive string constants and
  865. concatenates them into one, producing effectively
  866.  
  867.      do { if (x == 0) fprintf (stderr, "Warning: x == 0\n"); } while (0)
  868.  
  869. Stringification in C involves more than putting doublequote
  870. characters around the fragment; it is necessary to put backslashes in
  871. front of all doublequote characters, and all backslashes in string
  872. and character constants, in order to get a valid C string constant
  873. with the proper contents.  Thus, stringifying `p = "foo\n";' results
  874. in `"p = \"foo\\n\";"'.  However, backslashes that are not inside of
  875. string or character constants are not duplicated: `\n' by itself
  876. stringifies to `"\n"'.
  877.  
  878. Whitespace (including comments) in the text being stringified is
  879. handled according to precise rules.  All leading and trailing
  880. whitespace is ignored.  Any sequence of whitespace in the middle of
  881. the text is converted to a single space in the stringified result.
  882.  
  883.  
  884. 
  885. File: cpp.info,  Node: Concatenation,  Next: Undefining,  Prev: Stringification,  Up: Macros
  886.  
  887. Concatenation
  888. -------------
  889.  
  890. "Concatenation" means joining two strings into one.  In the context
  891. of macro expansion, concatenation refers to joining two lexical units
  892. into one longer one.  Specifically, an actual argument to the macro
  893. can be concatenated with another actual argument or with fixed text
  894. to produce a longer name.  The longer name might be the name of a
  895. function, variable or type, or a C keyword; it might even be the name
  896. of another macro, in which case it will be expanded.
  897.  
  898. When you define a macro, you request concatenation with the special
  899. operator `##' in the macro body.  When the macro is called, after
  900. actual arguments are substituted, all `##' operators are deleted, and
  901. so is any whitespace next to them (including whitespace that was part
  902. of an actual argument).  The result is to concatenate the syntactic
  903. tokens on either side of the `##'.
  904.  
  905. Consider a C program that interprets named commands.  There probably
  906. needs to be a table of commands, perhaps an array of structures
  907. declared as follows:
  908.  
  909.      struct command
  910.      {
  911.        char *name;
  912.        void (*function) ();
  913.      };
  914.      
  915.      struct command commands[] =
  916.      {
  917.        { "quit", quit_command},
  918.        { "help", help_command},
  919.        ...
  920.      };
  921.  
  922. It would be cleaner not to have to give each command name twice, once
  923. in the string constant and once in the function name.  A macro which
  924. takes the name of a command as an argument can make this unnecessary.
  925. The string constant can be created with stringification, and the
  926. function name by concatenating the argument with `_command'.  Here is
  927. how it is done:
  928.  
  929.      #define COMMAND(NAME)  { #NAME, NAME ## _command }
  930.      
  931.      struct command commands[] =
  932.      {
  933.        COMMAND (quit),
  934.        COMMAND (help),
  935.        ...
  936.      };
  937.  
  938. The usual case of concatenation is concatenating two names (or a name
  939. and a number) into a longer name.  But this isn't the only valid
  940. case.  It is also possible to concatenate two numbers (or a number
  941. and a name, such as `1.5' and `e3') into a number.  Also,
  942. multi-character operators such as `+=' can be formed by
  943. concatenation.  In some cases it is even possible to piece together a
  944. string constant.  However, two pieces of text that don't together
  945. form a valid lexical unit cannot be concatenated.  For example,
  946. concatenation with `x' on one side and `+' on the other is not
  947. meaningful because those two characters can't fit together in any
  948. lexical unit of C.  The ANSI standard says that such attempts at
  949. concatenation are undefined, but in the GNU C preprocessor it is well
  950. defined: it puts the `x' and `+' side by side with no particular
  951. special results.
  952.  
  953. Keep in mind that the C preprocessor converts comments to whitespace
  954. before macros are even considered.  Therefore, you cannot create a
  955. comment by concatenating `/' and `*': the `/*' sequence that starts a
  956. comment is not a lexical unit, but rather the beginning of a ``long''
  957. space character.  Also, you can freely use comments next to a `##' in
  958. a macro definition, or in actual arguments that will be concatenated,
  959. because the comments will be converted to spaces at first sight, and
  960. concatenation will later discard the spaces.
  961.  
  962.  
  963. 
  964. File: cpp.info,  Node: Undefining,  Next: Redefining,  Prev: Concatenation,  Up: Macros
  965.  
  966. Undefining Macros
  967. -----------------
  968.  
  969. To "undefine" a macro means to cancel its definition.  This is done
  970. with the `#undef' command.  `#undef' is followed by the macro name to
  971. be undefined.
  972.  
  973. Like definition, undefinition occurs at a specific point in the
  974. source file, and it applies starting from that point.  The name
  975. ceases to be a macro name, and from that point on it is treated by
  976. the preprocessor as if it had never been a macro name.
  977.  
  978. For example,
  979.  
  980.      #define FOO 4
  981.      x = FOO;
  982.      #undef FOO
  983.      x = FOO;
  984.  
  985. expands into
  986.  
  987.      x = 4;
  988.      
  989.      x = FOO;
  990.  
  991. In this example, `FOO' had better be a variable or function as well
  992. as (temporarily) a macro, in order for the result of the expansion to
  993. be valid C code.
  994.  
  995. The same form of `#undef' command will cancel definitions with
  996. arguments or definitions that don't expect arguments.  The `#undef'
  997. command has no effect when used on a name not currently defined as a
  998. macro.
  999.  
  1000.  
  1001. 
  1002. File: cpp.info,  Node: Redefining,  Next: Macro Pitfalls,  Prev: Undefining,  Up: Macros
  1003.  
  1004. Redefining Macros
  1005. -----------------
  1006.  
  1007. "Redefining" a macro means defining (with `#define') a name that is
  1008. already defined as a macro.
  1009.  
  1010. A redefinition is trivial if the new definition is transparently
  1011. identical to the old one.  You probably wouldn't deliberately write a
  1012. trivial redefinition, but they can happen automatically when a header
  1013. file is included more than once (*note Header Files::.), so they are
  1014. accepted silently and without effect.
  1015.  
  1016. Nontrivial redefinition is considered likely to be an error, so it
  1017. provokes a warning message from the preprocessor.  However, sometimes
  1018. it is useful to change the definition of a macro in mid-compilation. 
  1019. You can inhibit the warning by undefining the macro with `#undef'
  1020. before the second definition.
  1021.  
  1022. In order for a redefinition to be trivial, the new definition must
  1023. exactly match the one already in effect, with two possible exceptions:
  1024.  
  1025.    * Whitespace may be added or deleted at the beginning or the end.
  1026.  
  1027.    * Whitespace may be changed in the middle (but not inside strings).
  1028.      However, it may not be eliminated entirely, and it may not be
  1029.      added where there was no whitespace at all.
  1030.  
  1031. Recall that a comment counts as whitespace.
  1032.  
  1033.  
  1034. 
  1035. File: cpp.info,  Node: Macro Pitfalls,  Prev: Redefining,  Up: Macros
  1036.  
  1037. Pitfalls and Subtleties of Macros
  1038. ---------------------------------
  1039.  
  1040. In this section we describe some special rules that apply to macros
  1041. and macro expansion, and point out certain cases in which the rules
  1042. have counterintuitive consequences that you must watch out for.
  1043.  
  1044. * Menu:
  1045.  
  1046. * Misnesting::        Macros can contain unmatched parentheses.
  1047. * Macro Parentheses:: Why apparently superfluous parentheses
  1048.                          may be necessary to avoid incorrect grouping.
  1049. * Swallow Semicolon:: Macros that look like functions
  1050.                          but expand into compound statements.
  1051. * Side Effects::      Unsafe macros that cause trouble when
  1052.                          arguments contain side effects.
  1053. * Self-Reference::    Macros whose definitions use the macros' own names.
  1054. * Argument Prescan::  Actual arguments are checked for macro calls
  1055.                          before they are substituted.
  1056. * Cascaded Macros::   Macros whose definitions use other macros.
  1057.  
  1058.  
  1059. 
  1060. File: cpp.info,  Node: Misnesting,  Next: Macro Parentheses,  Prev: Macro Pitfalls,  Up: Macro Pitfalls
  1061.  
  1062. Improperly Nested Constructs
  1063. ............................
  1064.  
  1065.  Recall that when a macro is called with arguments, the arguments are
  1066. substituted into the macro body and the result is checked, together
  1067. with the rest of the input file, for more macro calls.
  1068.  
  1069. It is possible to piece together a macro call coming partially from
  1070. the macro body and partially from the actual arguments.  For example,
  1071.  
  1072.      #define double(x) (2*(x))
  1073.      #define call_with_1(x) x(1)
  1074.  
  1075. would expand `call_with_1 (double)' into `(2*(1))'.
  1076.  
  1077. Macro definitions do not have to have balanced parentheses.  By
  1078. writing an unbalanced open parenthesis in a macro body, it is
  1079. possible to create a macro call that begins inside the macro body but
  1080. ends outside of it.  For example,
  1081.  
  1082.      #define strange(file) fprintf (file, "%s %d",
  1083.      ...
  1084.      strange(stderr) p, 35)
  1085.  
  1086. This bizarre example expands to `fprintf (stderr, "%s %d", p, 35)'!
  1087.  
  1088.  
  1089. 
  1090. File: cpp.info,  Node: Macro Parentheses,  Next: Swallow Semicolon,  Prev: Misnesting,  Up: Macro Pitfalls
  1091.  
  1092. Unintended Grouping of Arithmetic
  1093. .................................
  1094.  
  1095.  You may have noticed that in most of the macro definition examples
  1096. shown above, each occurrence of a macro argument name had parentheses
  1097. around it.  In addition, another pair of parentheses usually surround
  1098. the entire macro definition.  Here is why it is best to write macros
  1099. that way.
  1100.  
  1101. Suppose you define a macro as follows,
  1102.  
  1103.      #define ceil_div(x, y) (x + y - 1) / y
  1104.  
  1105. whose purpose is to divide, rounding up.  (One use for this operation
  1106. is to compute how many `int''s are needed to hold a certain number of
  1107. `char''s.)  Then suppose it is used as follows:
  1108.  
  1109.      a = ceil_div (b & c, sizeof (int));
  1110.  
  1111. This expands into
  1112.  
  1113.      a = (b & c + sizeof (int) - 1) / sizeof (int);
  1114.  
  1115. which does not do what is intended.  The operator-precedence rules of
  1116. C make it equivalent to this:
  1117.  
  1118.      a = (b & (c + sizeof (int) - 1)) / sizeof (int);
  1119.  
  1120. But what we want is this:
  1121.  
  1122.      a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
  1123.  
  1124. Defining the macro as
  1125.  
  1126.      #define ceil_div(x, y) ((x) + (y) - 1) / (y)
  1127.  
  1128. provides the desired result.
  1129.  
  1130. However, unintended grouping can result in another way.  Consider
  1131. `sizeof ceil_div(1, 2)'.  That has the appearance of a C expression
  1132. that would compute the size of the type of `ceil_div (1, 2)', but in
  1133. fact it means something very different.  Here is what it expands to:
  1134.  
  1135.      sizeof ((1) + (2) - 1) / (2)
  1136.  
  1137. This would take the size of an integer and divide it by two.  The
  1138. precedence rules have put the division outside the `sizeof' when it
  1139. was intended to be inside.
  1140.  
  1141. Parentheses around the entire macro definition can prevent such
  1142. problems.  Here, then, is the recommended way to define `ceil_div':
  1143.  
  1144.      #define ceil_div(x, y) (((x) + (y) - 1) / (y))
  1145.  
  1146.  
  1147. 
  1148. File: cpp.info,  Node: Swallow Semicolon,  Next: Side Effects,  Prev: Macro Parentheses,  Up: Macro Pitfalls
  1149.  
  1150. Swallowing the Semicolon
  1151. ........................
  1152.  
  1153.  Often it is desirable to define a macro that expands into a compound
  1154. statement.  Consider, for example, the following macro, that advances
  1155. a pointer (the argument `p' says where to find it) across whitespace
  1156. characters:
  1157.  
  1158.      #define SKIP_SPACES (p, limit)  \
  1159.      { register char *lim = (limit); \
  1160.        while (p != lim) {            \
  1161.          if (*p++ != ' ') {          \
  1162.            p--; break; }}}
  1163.  
  1164. Here Backslash-Newline is used to split the macro definition, which
  1165. must be a single line, so that it resembles the way such C code would
  1166. be laid out if not part of a macro definition.
  1167.  
  1168. A call to this macro might be `SKIP_SPACES (p, lim)'.  Strictly
  1169. speaking, the call expands to a compound statement, which is a
  1170. complete statement with no need for a semicolon to end it.  But it
  1171. looks like a function call.  So it minimizes confusion if you can use
  1172. it like a function call, writing a semicolon afterward, as in
  1173. `SKIP_SPACES (p, lim);'
  1174.  
  1175. But this can cause trouble before `else' statements, because the
  1176. semicolon is actually a null statement.  Suppose you write
  1177.  
  1178.      if (*p != 0)
  1179.        SKIP_SPACES (p, lim);
  1180.      else ...
  1181.  
  1182.  The presence of two statements--the compound statement and a null
  1183. statement--in between the `if' condition and the `else' makes invalid
  1184. C code.
  1185.  
  1186. The definition of the macro `SKIP_SPACES' can be altered to solve
  1187. this problem, using a `do ... while' statement.  Here is how:
  1188.  
  1189.      #define SKIP_SPACES (p, limit)     \
  1190.      do { register char *lim = (limit); \
  1191.           while (p != lim) {            \
  1192.             if (*p++ != ' ') {          \
  1193.               p--; break; }}}           \
  1194.      while (0)
  1195.  
  1196. Now `SKIP_SPACES (p, lim);' expands into
  1197.  
  1198.      do {...} while (0);
  1199.  
  1200. which is one statement.
  1201.  
  1202.  
  1203. 
  1204. File: cpp.info,  Node: Side Effects,  Next: Self-Reference,  Prev: Swallow Semicolon,  Up: Macro Pitfalls
  1205.  
  1206. Duplication of Side Effects
  1207. ...........................
  1208.  
  1209.  Many C programs define a macro `min', for ``minimum'', like this:
  1210.  
  1211.      #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
  1212.  
  1213. When you use this macro with an argument containing a side effect, as
  1214. shown here,
  1215.  
  1216.      next = min (x + y, foo (z));
  1217.  
  1218. it expands as follows:
  1219.  
  1220.      next = ((x + y) < (foo (z)) ? (x + y) : (foo (z)));
  1221.  
  1222. where `x + y' has been substituted for `X' and `foo (z)' for `Y'.
  1223.  
  1224. The function `foo' is used only once in the statement as it appears
  1225. in the program, but the expression `foo (z)' has been substituted
  1226. twice into the macro expansion.  As a result, `foo' might be called
  1227. two times when the statement is executed.  If it has side effects or
  1228. if it takes a long time to compute, the results might not be what you
  1229. intended.  We say that `min' is an "unsafe" macro.
  1230.  
  1231. The best solution to this problem is to define `min' in a way that
  1232. computes the value of `foo (z)' only once.  The C language offers no
  1233. standard way to do this, but it can be done with GNU C extensions as
  1234. follows:
  1235.  
  1236.      #define min(X, Y)                     \
  1237.      ({ typeof (X) __x = (X), __y = (Y);   \
  1238.         (__x < __y) ? __x : __y; })
  1239.  
  1240. If you do not wish to use GNU C extensions, the only solution is to
  1241. be careful when *using* the macro `min'.  For example, you can
  1242. calculate the value of `foo (z)', save it in a variable, and use that
  1243. variable in `min':
  1244.  
  1245.      #define min(X, Y)  ((X) < (Y) ? (X) : (Y))
  1246.      ...
  1247.      {
  1248.        int tem = foo (z);
  1249.        next = min (x + y, tem);
  1250.      }
  1251.  
  1252. (where I assume that `foo' returns type `int').
  1253.  
  1254.  
  1255.